<template>
  <el-aside :width="width">
    <header @click="isFold" :style="{'width':width}">
      <i :class="icon"></i>
    </header>
    <el-scrollbar style="height:calc(100vh - 50px);">
      <el-menu
        :default-active="editableTabsValue"
        class="menu"
        :collapse="isCollapse"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
        :unique-opened="true"
        @select="activeMenu"
      >
        <el-submenu v-for="(item,idx) in menuList" :key="idx" :index="String(idx)">
          <template slot="title">
            <i class="el-icon-location"></i>
            <span slot="title">{{item.name}}</span>
          </template>
          <el-menu-item-group>
            <el-menu-item
              v-for="(item2,idx2) in item.list"
              :key="idx2"
              :index="String(idx + '-' + idx2)"
            >{{item2.name}}</el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
    </el-scrollbar>
  </el-aside>
</template>

<script>
export default {
  name: "vAside",
  data() {
    return {
      width: "200px", //折叠宽度
      icon: "el-icon-s-fold", //折叠图标
      isCollapse: false, //折叠开关
      menuList: [] //导航菜单
    };
  },
  computed: {
    editableTabsValue: {
      get() {
        return this.$store.state.editableTabsValue;
      },
      set(newVal) {
        this.$store.state.editableTabsValue = newVal;
      }
    }
  },
  mounted() {
    this.getMenuList(); //获取导航菜单
  },
  methods: {
    /* 折叠 */
    isFold() {
      if (this.width === "200px") {
        this.width = "64px";
        this.icon = "el-icon-s-unfold";
        this.isCollapse = true;
      } else {
        this.width = "200px";
        this.icon = "el-icon-s-fold";
        this.isCollapse = false;
      }
    },
    /* 获取导航菜单 */
    getMenuList() {
      this.$http.get("/user/info")
        .then(res => {
          if (res.data.code == 200) {
            this.menuList = res.data.menuList;
            this.$store.commit("SETMENULIST", this.menuList);
            this.$store.commit("SETROLELIST", res.data.roleList);
          }
        });
    },
    /* 点击菜单新增标签页 */
    activeMenu(index, indexPath, el) {
      const obj = {};
      obj.name = index;
      obj.title = el.$el.innerText;
      obj.index = indexPath;
      this.$store.commit("ADDTAB", obj);
    }
  }
};
</script>

<style lang="scss" scoped>
.el-aside {
  background-color: #545c64;
  header {
    height: 50px;
    line-height: 50px;
    text-align: center;
    background-color: #17b3a3;
    color: #fff;
    font-size: 28px;
    cursor: pointer;
    &:hover {
      background-color: #14988b;
    }
  }
  .menu {
    border: 0;
    height: calc(100vh - 50px);
  }
  .menu:not(.el-menu--collapse) {
    width: 200px;
  }
  &,
  header {
    transition: width 1s;
    -webkit-transition: width 1s; /* Safari */
  }
  /deep/ .el-menu-item,
  .el-submenu__title {
    height: 40px;
    line-height: 40px;
  }
  /deep/ .el-menu-item-group__title {
    padding: 0 0 0 20px;
  }
  /deep/ .el-scrollbar__wrap {
    overflow-x: hidden;
  }
}
</style>